<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

namespace app\controller;
use think\facade\Db;
class ApiLipin extends ApiCommon
{	
	public function initialize(){
		parent::initialize();
		$this->checklogin();
	}
	public function index(){
		
		$lipinset = Db::name('lipin_set')->where('aid',aid)->find();
		if(!$lipinset) $lipinset = ['status'=>1,'scanshow'=>0,'guize'=>''];
		if($lipinset['status'] == 0){
			return $this->json(['status'=>-4,'msg'=>'功能未开启']);
		}

		if(request()->isPost()){
			$dhcode = input('param.dhcode');
			$errnum = cache($this->sessionid.'_lipincode_errnum');
			$errnum = intval($errnum);
			if($errnum > 5) return $this->json(['status'=>0,'msg'=>'输入错误次数过多，请稍后再试']);
			if(false){}else{
				$codeinfo = Db::name('lipin_codelist')->where('aid',aid)->where('code',$dhcode)->find();
			}
			if(!$codeinfo){
				$errnum++;
				cache($this->sessionid.'_lipincode_errnum',$errnum,600);
				return $this->json(['status'=>0,'msg'=>'兑换码输入错误']);
			}
			if($codeinfo['status'] == 1){
				return $this->json(['status'=>0,'msg'=>'兑换码已使用']);
			}
			$hid = $codeinfo['hid'];
			$hdinfo = Db::name('lipin')->where('id',$hid)->where('aid',aid)->find();
			if(!$hdinfo || $hdinfo['status'] == 0) return $this->json(['status'=>0,'msg'=>'活动未开启']);
			if($hdinfo['starttime'] > time()) return $this->json(['status'=>0,'msg'=>'活动尚未开始']);
			if($hdinfo['endtime'] < time()) return $this->json(['status'=>0,'msg'=>'活动已结束']);

            if($hdinfo['type']==0){ //余额
				$money = $hdinfo['money'];
                Db::name('lipin_codelist')->where('id',$codeinfo['id'])->update(['status'=>1,'usetime'=>time(),'mid'=>mid,'headimg'=>$this->member['headimg'],'nickname'=>$this->member['nickname'],'remark'=>'兑换'.t('余额').$money.'元']);
                \app\common\Member::addmoney(aid,mid,$money,$hdinfo['name']);
				return $this->json(['status'=>1,'msg'=>'成功兑换'.t('余额').$money.'元','url'=>true]);
			}elseif($hdinfo['type']==2){ //积分
				$score = $hdinfo['score'];
                Db::name('lipin_codelist')->where('id',$codeinfo['id'])->update(['status'=>1,'usetime'=>time(),'mid'=>mid,'headimg'=>$this->member['headimg'],'nickname'=>$this->member['nickname'],'remark'=>'兑换'.t('积分').$score.'个']);
                \app\common\Member::addscore(aid,mid,$score,$hdinfo['name']);
				return $this->json(['status'=>1,'msg'=>'成功兑换'.t('积分').$score.'个','url'=>true]);
			}elseif($hdinfo['type']==3){ //优惠券
			    if($hdinfo['coupon_ids']) {
					$coupon_ids = explode(',', $hdinfo['coupon_ids']);
					if($coupon_ids) {
						foreach($coupon_ids as $coupon_id){
							\app\common\Coupon::send(aid,mid,$coupon_id);
						}
						$tmpl_re = "成功兑换优惠券".count($coupon_ids).'张';
					}
				}
				Db::name('lipin_codelist')->where('id',$codeinfo['id'])->update(['status'=>1,'usetime'=>time(),'mid'=>mid,'headimg'=>$this->member['headimg'],'nickname'=>$this->member['nickname'],'remark'=>$tmpl_re]);
				return $this->json(['status'=>3,'msg'=>$tmpl_re,'url'=>'/pages/coupon/mycoupon']);
			}else{ //商品
				return $this->json(['status'=>2,'type'=>$hdinfo['type'],'msg'=>'验证成功，正在前往兑换']);
			}
		}
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['lipinset'] = $lipinset;
		return $this->json($rdata);
	}
	
	public function dhlog(){
		$pagenum = input('post.pagenum');
        $st = input('post.st');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$where[] = ['status','=',1];
		$field = 'id,code,remark,from_unixtime(usetime)createtime';
		$datalist = Db::name('lipin_codelist')->field($field)->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		if(request()->isPost()){
			return $this->json(['status'=>1,'data'=>$datalist]);
		}
		
		$count = Db::name('lipin_codelist')->where($where)->count();

		$rdata = [];
		$rdata['count'] = $count;
		$rdata['datalist'] = $datalist;
		$rdata['pernum'] = $pernum;
		$rdata['st'] = $st;
		$rdata['myscore'] = $this->member['score'];
		return $this->json($rdata);
	}
	//兑换商品
	public function prodh(){
		$lipinset = Db::name('lipin_set')->where('aid',aid)->find();
		if($lipinset['status'] == 0){
			return $this->json(['status'=>-4,'msg'=>'功能未开启']);
		}
		
		$dhcode = input('param.dhcode');
		$errnum = cache($this->sessionid.'_lipincode_errnum');
		$errnum = intval($errnum);
		if($errnum > 5) return $this->json(['status'=>0,'msg'=>'输入错误次数过多，请稍后再试']);

		if(false){}else{
			$codeinfo = Db::name('lipin_codelist')->where('aid',aid)->where('code',$dhcode)->find();
		}
		if(!$codeinfo){
			$errnum++;
			cache($this->sessionid.'_lipincode_errnum',$errnum,600);
			return $this->json(['status'=>0,'msg'=>'兑换码输入错误']);
		}
		if($codeinfo['status'] == 1){
			return $this->json(['status'=>0,'msg'=>'兑换码已使用']);
		}
		$hid = $codeinfo['hid'];
		$hdinfo = Db::name('lipin')->where('id',$hid)->where('aid',aid)->find();
		if(!$hdinfo || $hdinfo['status'] == 0) return $this->json(['status'=>0,'msg'=>'活动未开启']);
		if($hdinfo['starttime'] > time()) return $this->json(['status'=>0,'msg'=>'活动尚未开始']);
		if($hdinfo['endtime'] < time()) return $this->json(['status'=>0,'msg'=>'活动已结束']);
		if($hdinfo['type']!=1 && $hdinfo['type']!=4) return $this->json(['status'=>0,'msg'=>'兑换类型不是商品']);
		
		if($hdinfo['type']==4){
			$prodata = explode('-',$hdinfo['prodata4']);
			$hdinfo['num_type'] = $hdinfo['num_type4'];
		}else{
			$prodata = explode('-',$hdinfo['prodata']);
		}
		$address = Db::name('member_address')->where('aid',aid)->where('mid',mid)->order('isdefault desc,id desc')->find();
		if(!$address) $address = array();
		
		$userinfo = [];
		$userinfo['realname'] = $this->member['realname'];
		$userinfo['tel'] = $this->member['tel'];

		$allbuydata = [];
		$autofahuo = 0;
		foreach($prodata as $key=>$gwc){
			list($proid,$ggid,$num) = explode(',',$gwc);

			if($hdinfo['type']==4){
				$product = Db::name('scoreshop_product')->field("id,aid,bid,cid,pic,name,sales,stock,money_price sell_price,score_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata")->where('aid',aid)->where('status',1)->where('ischecked',1)->where('id',$proid)->find();
				
				if(!$product){
					Db::name('scoreshop_cart')->where('aid',aid)->where('proid',$proid)->delete();
					return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
				}
				if($product['freighttype'] == 3 || $product['freighttype'] == 4) $autofahuo = $product['freighttype'];
				if($ggid){
					$guige = Db::name('scoreshop_guige')->where('id',$ggid)->find();
					if(!$guige){
						Db::name('scoreshop_guige')->where('aid',aid)->where('ggid',$ggid)->delete();
						return $this->json(['status'=>0,'msg'=>'产品该规格不存在或已下架']);
					}
					$guige['sell_price'] = $guige['money_price'];
				}else{
					$guige = ['id'=>'0','name'=>'默认规格','pic'=>'','sell_price'=>$product['sell_price'],'score_price'=>$product['score_price'],'weight'=>$product['weight'],'stock'=>$product['stock'],'ks'=>'0'];
				}
				if($guige['stock'] < $num){
					return $this->json(['status'=>0,'msg'=>'库存不足!']);
				}
				$price = round($guige['sell_price'] * $num,2);
				$score = round($guige['score_price'] * $num);
			}else{
				$product = Db::name('shop_product')->field("id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata,perlimit,gettj,gettjtip,gettjurl,scoredkmaxset,scoredkmaxval")->where('aid',aid)->where('status',1)->where('ischecked',1)->where('id',$proid)->find();
				
				if(!$product){
					Db::name('shop_cart')->where('aid',aid)->where('proid',$proid)->delete();
					return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
				}
				if($product['freighttype'] == 3 || $product['freighttype'] == 4) $autofahuo = $product['freighttype'];
				$guige = Db::name('shop_guige')->where('id',$ggid)->find();
				if(!$guige){
					Db::name('shop_cart')->where('aid',aid)->where('ggid',$ggid)->delete();
					return $this->json(['status'=>0,'msg'=>'产品该规格不存在或已下架']);
				}
				if($guige['stock'] < $num){
					return $this->json(['status'=>0,'msg'=>'库存不足']);
				}
				$gettj = explode(',',$product['gettj']);
				if(!in_array('-1',$gettj) && !in_array($this->member['levelid'],$gettj) && (!in_array('0',$gettj) || $this->member['subscribe']!=1)){ //不是所有人
					if(platform == 'wx'){
						return $this->json(['status'=>0,'msg'=>$product['gettjtip'],'url'=>$product['gettjurl']]);
					}
				}
				$price = round($guige['sell_price'] * $num,2);
				$score = 0;
			}

			if(!$allbuydata[$product['bid']]) $allbuydata[$product['bid']] = [];
			if(!$allbuydata[$product['bid']]['prodata']) $allbuydata[$product['bid']]['prodata'] = [];
			$allbuydata[$product['bid']]['prodata'][] = ['product'=>$product,'guige'=>$guige,'num'=>$num,'price'=>$price,'score'=>$score];
		}
		if($autofahuo>0 && count($prodata) > 1){
			return $this->json(['status'=>0,'msg'=>'虚拟商品和实物商品不能一起兑换']);
		}
		
		$havetongcheng = 0;
		foreach($allbuydata as $bid=>$buydata){
			if($autofahuo>0){
				$freightList = [['id'=>0,'name'=>($autofahuo==3?'自动发货':'在线卡密'),'pstype'=>$autofahuo]];
			}else{
				$freightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);
				$fids = [];
				foreach($freightList as $v){
					$fids[] = $v['id'];
				}
				foreach($buydata['prodata'] as $prodata){
					if($prodata['product']['freighttype']==0){
						$fids = array_intersect($fids,explode(',',$prodata['product']['freightdata']));
					}else{
						$thisfreightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid]]);
						$thisfids = [];
						foreach($thisfreightList as $v){
							$thisfids[] = $v['id'];
						}
						$fids = array_intersect($fids,$thisfids);
					}
				}
				if(!$fids){
					if(count($buydata['prodata'])>1){
						return $this->json(['status'=>0,'msg'=>'所选择商品配送方式不同，请分别下单']);
					}else{
						return $this->json(['status'=>0,'msg'=>'获取配送方式失败']);
					}
				}
				$freightList = \app\model\Freight::getList([['status','=',1],['aid','=',aid],['bid','=',$bid],['id','in',$fids]]);
				foreach($freightList as $k=>$v){
					if($v['pstype']==2){ //同城配送
						$havetongcheng = 1;
					}
				}
			}
			$allbuydata[$bid]['freightList'] = $freightList;
		}
		if($havetongcheng){
			$address = Db::name('member_address')->where('aid',aid)->where('mid',mid)->where('latitude','>',0)->order('isdefault desc,id desc')->find();
		}else{
			$address = Db::name('member_address')->where('aid',aid)->where('mid',mid)->order('isdefault desc,id desc')->find();
		}
		if(!$address) $address = [];
		$needLocation = 0;
		$allproduct_price = 0;
		foreach($allbuydata as $bid=>$buydata){
			if($bid!=0){
				$business = Db::name('business')->where('id',$bid)->field('id,aid,cid,name,logo,tel,address,sales,longitude,latitude')->find();
			}else{
				$business = Db::name('admin_set')->where('aid',aid)->field('id,name,logo,desc,tel')->find();
			}
			
			$product_price = 0;
			$score_price = 0;
			$totalweight = 0;
			$totalnum = 0;
			$prodataArr = [];
			foreach($buydata['prodata'] as $prodata){
				$product_price += $prodata['guige']['sell_price'] * $prodata['num'];
				$totalweight += $prodata['guige']['weight'] * $prodata['num'];
				$totalnum += $prodata['num'];
				$score_price += $prodata['score'];
				$prodataArr[] = $prodata['product']['id'].','.$prodata['guige']['id'].','.$prodata['num'];
			}
			$prodatastr = implode('-',$prodataArr);
			
			$rs = \app\model\Freight::formatFreightList($buydata['freightList'],$address,$product_price,$totalnum,$totalweight);

			$freightList = $rs['freightList'];
			$freightArr = $rs['freightArr'];
			if($rs['needLocation']==1) $needLocation = 1;
			
			
			$allbuydata[$bid]['bid'] = $bid;
			$allbuydata[$bid]['business'] = $business;
			$allbuydata[$bid]['prodatastr'] = $prodatastr;
//			$allbuydata[$bid]['couponList'] = $couponList;
//			$allbuydata[$bid]['couponCount'] = count($couponList);
			$allbuydata[$bid]['freightList'] = $freightList;
			$allbuydata[$bid]['freightArr'] = $freightArr;
			$allbuydata[$bid]['product_price'] = round($product_price,2);
			$allbuydata[$bid]['score_price'] = round($score_price);
//			$allbuydata[$bid]['leveldk_money'] = $leveldk_money;
			$allbuydata[$bid]['coupon_money'] = 0;
			$allbuydata[$bid]['coupontype'] = 1;
			$allbuydata[$bid]['couponrid'] = 0;
			$allbuydata[$bid]['freightkey'] = 0;
			$allbuydata[$bid]['pstimetext'] = '';
			$allbuydata[$bid]['freight_time'] = '';
			$allbuydata[$bid]['storeid'] = 0;
			$allbuydata[$bid]['storename'] = '';
			$allbuydata[$bid]['message'] = '';
			$allbuydata[$bid]['field1'] = '';
			$allbuydata[$bid]['field2'] = '';
			$allbuydata[$bid]['field3'] = '';
			$allbuydata[$bid]['field4'] = '';
			$allbuydata[$bid]['field5'] = '';
//			$allbuydata[$bid]['cuxiaolist'] = $newcxlist;
//			$allbuydata[$bid]['cuxiaoCount'] = count($newcxlist);
			$allbuydata[$bid]['cuxiao_money'] = 0;
			$allbuydata[$bid]['cuxiaotype'] = 0;
			$allbuydata[$bid]['cuxiaoid'] = 0;
			$allbuydata[$bid]['editorFormdata'] = [];

			$allproduct_price += $product_price;
		}
		
		$rdata = [];
		$rdata['status'] = 1;
		$rdata['havetongcheng'] = $havetongcheng;
		$rdata['address'] = $address;
		$rdata['linkman'] = $address ? $address['name'] : strval($userinfo['realname']);
		$rdata['tel'] = $address ? $address['tel'] : strval($userinfo['tel']);
		$rdata['userinfo'] = $userinfo;
		$rdata['allbuydata'] = $allbuydata;
		$rdata['needLocation'] = $needLocation;

		$rdata['hdinfo'] = $hdinfo;

		return $this->json($rdata);
	}

	public function createOrder(){

		$lipinset = Db::name('lipin_set')->where('aid',aid)->find();
		if(!$lipinset) $lipinset = ['status'=>1,'scanshow'=>0,'guize'=>''];
		if($lipinset['status'] == 0){
			return $this->json(['status'=>-4,'msg'=>'功能未开启']);
		}

		$dhcode = input('param.dhcode');
		$errnum = cache($this->sessionid.'_lipincode_errnum');
		$errnum = intval($errnum);
		if($errnum > 5) return $this->json(['status'=>0,'msg'=>'输入错误次数过多，请稍后再试']);
		if(false){}else{
			$codeinfo = Db::name('lipin_codelist')->where('aid',aid)->where('code',$dhcode)->find();
		}
		if(!$codeinfo){
			$errnum++;
			cache($this->sessionid.'_lipincode_errnum',$errnum,600);
			return $this->json(['status'=>0,'msg'=>'兑换码输入错误']);
		}
		if($codeinfo['status'] == 1){
			return $this->json(['status'=>0,'msg'=>'兑换码已使用']);
		}
		$hid = $codeinfo['hid'];
		$hdinfo = Db::name('lipin')->where('id',$hid)->where('aid',aid)->find();
		if(!$hdinfo || $hdinfo['status'] == 0) return $this->json(['status'=>0,'msg'=>'活动未开启']);
		if($hdinfo['starttime'] > time()) return $this->json(['status'=>0,'msg'=>'活动尚未开始']);
		if($hdinfo['endtime'] < time()) return $this->json(['status'=>0,'msg'=>'活动已结束']);
		if($hdinfo['type']!=1 && $hdinfo['type']!=4) return $this->json(['status'=>0,'msg'=>'兑换类型不是商品']);

		if($hdinfo['type']==4){
			$hdinfo['num_type'] = $hdinfo['num_type4'];
			$hdinfo['prodata'] = $hdinfo['prodata4'];
		}

		$post = input('post.');
		//收货地址
		if($post['addressid']=='' || $post['addressid']==0){
			$address = ['id'=>0,'name'=>$post['linkman'],'tel'=>$post['tel'],'area'=>'','address'=>''];
		}else{
			$address = Db::name('member_address')->where('id',$post['addressid'])->where('aid',aid)->where('mid',mid)->find();
		}

        $allbuydata = [];
        $postdata = input('post.buydata/a');
        $pro_kind=0;
        $goods_name = '';//兑换的商品信息
        foreach ($postdata as $pdata){
            $buydata = explode('-',$pdata['prodata']);
            foreach($buydata as $key=>$gwc){
                $pro_kind ++;
                list($proid,$ggid,$num) = explode(',',$gwc);

				if($hdinfo['type']==4){
					$product = Db::name('scoreshop_product')->field("id,aid,bid,cid,pic,name,sales,stock,money_price sell_price,score_price,lvprice,lvprice_data,sellpoint,fuwupoint,freighttype,freightdata")->where('aid',aid)->where('status',1)->where('ischecked',1)->where('id',$proid)->find();
					if(!$product){
						return $this->json(['status'=>0,'msg'=>'产品不存在或已下架']);
					}
					if($ggid){
						$guige = Db::name('scoreshop_guige')->where('id',$ggid)->find();
						if(!$guige){
							Db::name('scoreshop_guige')->where('aid',aid)->where('ggid',$ggid)->delete();
							return $this->json(['status'=>0,'msg'=>'产品该规格不存在或已下架']);
						}
						$guige['sell_price'] = $guige['money_price'];
					}else{
						$guige = ['id'=>'0','name'=>'默认规格','pic'=>'','sell_price'=>$product['sell_price'],'score_price'=>$product['score_price'],'weight'=>$product['weight'],'stock'=>$product['stock'],'ks'=>'0'];
					}
					if($guige['stock'] < $num){
						return $this->json(['status'=>0,'msg'=>'库存不足!']);
					}
				}else{
					$product = Db::name('shop_product')->field("id,aid,bid,cid,pic,name,sales,market_price,sell_price,lvprice,lvprice_data,freighttype,freightdata,perlimit,isjici")->where('aid',aid)->where('id',$proid)->find();
					if(!$product){
						return $this->json(['status'=>0,'msg'=>'产品不存在']);
					}
					$guige = Db::name('shop_guige')->where('id',$ggid)->find();
					if(!$guige){
						Db::name('shop_cart')->where('aid',aid)->where('ggid',$ggid)->delete();
						return $this->json(['status'=>0,'msg'=>'产品该规格不存在']);
					}
					if($guige['stock'] < $num){
						return $this->json(['status'=>0,'msg'=>'库存不足']);
					}
				}
                if(!$allbuydata[$product['bid']]) $allbuydata[$product['bid']] = [];
                if(!$allbuydata[$product['bid']]['prodata']) $allbuydata[$product['bid']]['prodata'] = [];
                $allbuydata[$product['bid']]['prodata'][] = ['product'=>$product,'guige'=>$guige,'num'=>$num];

                if($goods_name){
                	//查询字数是否超出100字
	                $gn_len = mb_strlen($goods_name." | ".$product['name']);
	                if($gn_len<=100){
	                	$goods_name .= " | ".$product['name'];
	                }else{
	                	//查询是否有等字，没则添加
	                	$d_len = strrpos($goods_name,"等");
	                	if(!$d_len && $d_len !==0){
	                		$goods_name .= "等";
	                	}
	                }
                }else{
                	$goods_name .= $product['name'];
                }

            }
        }

        $ordernum = date('ymdHis').rand(100000,999999);

		$i = 0;
		foreach($allbuydata as $bid=>$allprodata){
			foreach($postdata as $v){
				if($v['bid'] == $bid){
					$data = $v;
				}
			}
			$i++;
			$product_price = 0;
			$weight = 0;//重量
			$goodsnum = 0;
			$prolist = [];
			$proids = [];
			$cids = [];

			$freightList = Db::name('freight')->where('status',1)->where('aid',aid)->where('bid',$bid)->order('sort desc,id')->select()->toArray();
			if(!$freightList) $freightList = [['id'=>0,'name'=>'包邮','pstype'=>0,'pricedata'=>'[{"region":"全国(默认运费)","fristweight":"1000","fristprice":"0","secondweight":"1000","secondprice":"0"}]']];
			$fids = [];
			foreach($freightList as $v){
				$fids[] = $v['id'];
			}
			foreach($allprodata['prodata'] as $key=>$prodata){
				$product = $prodata['product'];
				if($key==0) $title = $product['name'];
				$guige = $prodata['guige'];
				$num = $prodata['num'];
				$product_price += $guige['sell_price'] * $num;
				$weight += $guige['weight'] * $num;
				$goodsnum += $num;

				$prolist[] = ['product'=>$product,'guige'=>$guige,'num'=>$num];
				
				if($product['freighttype']==0){
					$fids = array_intersect($fids,explode(',',$product['freightdata']));
				}else{
					$thisfreightList = Db::name('freight')->where('status',1)->where('aid',aid)->where('bid',$bid)->where('pstype','<>',3)->order('sort desc,id')->select()->toArray();
					if(!$thisfreightList){
						$thisfreightList = [
							['id'=>0,'name'=>'包邮','pstype'=>0,'pricedata'=>'[{"region":"全国(默认运费)","fristweight":"1000","fristprice":"0","secondweight":"1000","secondprice":"0"}]']
						];
					}

					$thisfids = [];
					foreach($thisfreightList as $v){
						$thisfids[] = $v['id'];
					}
					$fids = array_intersect($fids,$thisfids);
				}

				$proids[] = $product['id'];
				$cids[] = $product['cid'];
			}
			if(!$fids){
				if(count($prolist)>1){
					return $this->json(['status'=>0,'msg'=>'所选择商品配送方式不同，请联系商家']);
				}else{
					return $this->json(['status'=>0,'msg'=>'获取配送方式失败']);
				}
			}
			$totalprice = $product_price;

			if($data['freight_id']){
				$freight = Db::name('freight')->where('aid',aid)->where('id',$data['freight_id'])->find();
				
				if(($address['name']=='' || $address['tel'] =='') && ($freight['pstype']==1 || $freight['pstype']==3) && $freight['needlinkinfo']==1){
					return $this->json(['status'=>0,'msg'=>'请填写联系人和联系电话']);
				}
				$rs = \app\model\Freight::getFreightPrice($freight,$address,$product_price,$totalnum,$totalweight);
				if($rs['status']==0) return $this->json($rs);

				//判断配送时间选择是否符合要求
				if($freight['pstimeset']==1){
					$freight_times = explode('~',$data['freight_time']);
					if($freight_times[1]){
						$freighttime = strtotime(explode(' ',$freight_times[0])[0] . ' '.$freight_times[1]);
					}else{
						$freighttime = strtotime($freight_times[0]);
					}
					if(time() + $freight['psprehour']*3600 > $freighttime){
						return $this->json(['status'=>0,'msg'=>(($freight['pstype']==0 || $freight['pstype']==2 || $freight['pstype']==10)?'配送':'提货').'时间必须在'.$freight['psprehour'].'小时之后']);
					}
				}
			}elseif($product['freighttype']==3){
				$freight = ['id'=>0,'name'=>'自动发货','pstype'=>3];
			}elseif($product['freighttype']==4){
				$freight = ['id'=>0,'name'=>'在线卡密','pstype'=>4];
			}else{
				$freight = ['id'=>0,'name'=>'包邮','pstype'=>0];
			}
			//积分抵扣
			$dscore = 0;
			$scoredk = 0;
			
			$orderdata = [];
			$orderdata['aid'] = aid;
			$orderdata['mid'] = mid;
			$orderdata['bid'] = $data['bid'];
			if(count($allbuydata) > 1){
				$orderdata['ordernum'] = $ordernum.'_'.$i;
			}else{
				$orderdata['ordernum'] = $ordernum;
			}
			$orderdata['title'] = $title.(count($prodata)>1?'等':'');
			
			$orderdata['linkman'] = $address['name'];
			$orderdata['tel'] = $address['tel'];
			$orderdata['area'] = $address['area'];
			$orderdata['area2'] = $address['province'].','.$address['city'].','.$address['district'];
			$orderdata['address'] = $address['address'];
			$orderdata['totalprice'] = 0;
			if($hdinfo['type']==1){
				$orderdata['product_price'] = $product_price;
			}
			$orderdata['message'] = $data['message'];
            $freight_price = 0; //todo
			if($freight && $freight['pstype']==0){ //快递
				$orderdata['freight_text'] = $freight['name'].'('.$freight_price.'元)';
			}elseif($freight && $freight['pstype']==1){ //到店自提
				$orderdata['mdid'] = $data['storeid'];
				if($data['bid']!=0){
					$storename = Db::name('business')->where('aid',aid)->where('id',$data['bid'])->value('name');
				}else{
					$storename = Db::name('mendian')->where('aid',aid)->where('id',$data['storeid'])->value('name');
				}
				$orderdata['freight_text'] = $freight['name'].'['.$storename.']';
				$orderdata['freight_type'] = 1;
				}elseif($freight && $freight['pstype']==2){ //同城配送
				$orderdata['freight_text'] = $freight['name'].'('.$freight_price.'元)';
				$orderdata['freight_type'] = 2;
			}elseif($freight && ($freight['pstype']==3 || $freight['pstype']==4)){ //自动发货 在线卡密
				$orderdata['freight_text'] = $freight['name'];
				$orderdata['freight_type'] = $freight['pstype'];
			}else{
				$orderdata['freight_text'] = '包邮';
			}
			$orderdata['freight_id'] = $freight['id'];
			$orderdata['freight_time'] = $data['freight_time']; //配送时间
			$orderdata['createtime'] = time();
			$orderdata['platform'] = platform;
			$orderdata['hexiao_code'] = random(16);

			$orderdata['status'] = 1;
			$orderdata['paytype'] = '兑换码兑换';
			$orderdata['paytime'] = time();
			if($hdinfo['type']==4){
				$orderdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=scoreshop&co='.$orderdata['hexiao_code']));
				$orderid = Db::name('scoreshop_order')->insertGetId($orderdata);
				\app\model\Freight::saveformdata($orderid,'scoreshop_order',$freight['id'],$data['formdata']);
			}else{
				$orderdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shop&co='.$orderdata['hexiao_code']));
				$orderid = Db::name('shop_order')->insertGetId($orderdata);
				\app\model\Freight::saveformdata($orderid,'shop_order',$freight['id'],$data['formdata']);
			}
			
			
            foreach($prolist as $key=>$v){
				$product = $v['product'];
				$guige = $v['guige'];
				$num = $v['num'];
				$ogdata = [];
				$ogdata['aid'] = aid;
				$ogdata['bid'] = $product['bid'];
				$ogdata['mid'] = mid;
				$ogdata['orderid'] = $orderid;
				$ogdata['ordernum'] = $orderdata['ordernum'];
				$ogdata['proid'] = $product['id'];
				$ogdata['name'] = $product['name'];
				$ogdata['pic'] = $product['pic'];
				$ogdata['procode'] = $product['procode'];
				$ogdata['ggid'] = $guige['id'];
				$ogdata['ggname'] = $guige['name'];
				if($hdinfo['type']==1){
					$ogdata['cid'] = $product['cid'];
				}
				$ogdata['num'] = $num;
				$ogdata['cost_price'] = $guige['cost_price'];
				$ogdata['sell_price'] = $guige['sell_price'];
				if($hdinfo['type']==4){
					$ogdata['totalscore'] = $num * $guige['score_price'];
					$ogdata['totalmoney'] = $num * $guige['sell_price'];
					$ogdata['money_price'] = $guige['sell_price'];
					$ogdata['score_price'] = $guige['score_price'];
				}else{
					$ogdata['totalprice'] = $num * $guige['sell_price'];
				}
				$ogdata['status'] = 1;
				$ogdata['createtime'] = time();

				if($product['isjici'] == 1){
					$ogdata['hexiao_code'] = random(18);
					$ogdata['hexiao_qr'] = createqrcode(m_url('admin/hexiao/hexiao?type=shopproduct&co='.$ogdata['hexiao_code']));
				}

				$ogtotalprice = $ogdata['totalprice'];
				
				if($hdinfo['type']==4){
					$ogid = Db::name('scoreshop_order_goods')->insertGetId($ogdata);
					if($guige['id']){
						Db::name('scoreshop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
					}
					Db::name('scoreshop_product')->where('aid',aid)->where('id',$product['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
				}else{
					$ogid = Db::name('shop_order_goods')->insertGetId($ogdata);

					Db::name('shop_guige')->where('aid',aid)->where('id',$guige['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
					Db::name('shop_product')->where('aid',aid)->where('id',$product['id'])->update(['stock'=>Db::raw("stock-$num"),'sales'=>Db::raw("sales+$num")]);
				}
			}

			if($orderdata['status'] == 1){
				if($hdinfo['type']==4){
					\app\model\Payorder::scoreshop_pay($orderid);
					$tmplcontent = [];
					$tmplcontent['first'] = '有新的兑换订单下单成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $ordernum;//订单号
					$tmplcontent['keyword3'] = $orderdata['totalscore'].t('积分').($orderdata['totalprice']>0?' + '.$orderdata['totalprice'].'元':'');//订单金额
					$tmplcontent['keyword4'] = $orderdata['title'];//商品信息
                    $tmplcontentNew = [];
                    $tmplcontentNew['thing8'] = \app\common\Mendian::getNameWithBusines($orderdata);//门店
                    $tmplcontentNew['phrase18'] = $this->member['nickname']; //用户名
                    $tmplcontentNew['character_string2'] = $ordernum;//订单号
                    $tmplcontentNew['amount5'] = $orderdata['totalscore'].t('积分').($orderdata['totalprice']>0?' + '.$orderdata['totalprice'].'元':'');//订单金额
                    $tmplcontentNew['thing3'] = $orderdata['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,$orderdata['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/scoreshoporder'),$orderdata['mdid'],$tmplcontentNew);

					$tmplcontent = [];
					$tmplcontent['thing11'] = $orderdata['title'];
					$tmplcontent['character_string2'] = $orderdata['ordernum'];
					$tmplcontent['phrase10'] = '已支付';
					$tmplcontent['amount13'] = $orderdata['totalprice'].'元';
					$tmplcontent['thing27'] = $this->member['nickname'];
					\app\common\Wechat::sendhtwxtmpl(aid,$orderdata['bid'],'tmpl_orderpay',$tmplcontent,'admin/order/scoreshoporder',$orderdata['mdid']);
				}else{
					\app\model\Payorder::shop_pay($orderid);
					$tmplcontent = [];
					$tmplcontent['first'] = '有新的兑换订单下单成功';
					$tmplcontent['remark'] = '点击进入查看~';
					$tmplcontent['keyword1'] = $this->member['nickname']; //用户名
					$tmplcontent['keyword2'] = $ordernum;//订单号
					$tmplcontent['keyword3'] = $orderdata['totalprice'].'元';//订单金额
					$tmplcontent['keyword4'] = $orderdata['title'];//商品信息
                    $tmplcontentNew = [];
                    $tmplcontentNew['thing8'] = \app\common\Mendian::getNameWithBusines($orderdata);//门店
                    $tmplcontentNew['phrase18'] = $this->member['nickname']; //用户名
                    $tmplcontentNew['character_string2'] = $ordernum;//订单号
                    $tmplcontentNew['amount5'] = $orderdata['totalprice'];//订单金额
                    $tmplcontentNew['thing3'] = $orderdata['title'];//商品信息
					\app\common\Wechat::sendhttmpl(aid,$orderdata['bid'],'tmpl_orderpay',$tmplcontent,m_url('admin/order/shoporder'),$orderdata['mdid'],$tmplcontentNew);

					$tmplcontent = [];
					$tmplcontent['thing11'] = $orderdata['title'];
					$tmplcontent['character_string2'] = $orderdata['ordernum'];
					$tmplcontent['phrase10'] = '已支付';
					$tmplcontent['amount13'] = $orderdata['totalprice'].'元';
					$tmplcontent['thing27'] = $this->member['nickname'];
					\app\common\Wechat::sendhtwxtmpl(aid,$orderdata['bid'],'tmpl_orderpay',$tmplcontent,'admin/order/shoporder',$orderdata['mdid']);
				}
			}
		}

		//使用状态
		$usestatus = 1;
		if($usestatus){
        	Db::name('lipin_codelist')->where('id',$codeinfo['id'])->update(['status'=>1,'usetime'=>time(),'mid'=>mid,'headimg'=>$this->member['headimg'],'nickname'=>$this->member['nickname'],'remark'=>'兑换商品:'.$goods_name]);
			return $this->json(['status'=>1,'orderid'=>0,'ordercount'=>0,'ordernum'=>$ordernum,'msg'=>'兑换成功']);
        }else{
        	}
		
	}
}